package com.tongguan.paypos.utils;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.StringRes;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.tongguan.paypos.R;
import com.tongguan.paypos.utils.DensityUtil;


/**
 * Created by Joylive on 2016/9/14 .
 */
public class ToastUtils {
    private static Drawable iconLeft;
    private static Toast mToastView;
    private static View toastContentView;
    private static ImageView toastIconView;
    private static TextView toastMessageView;

    private static Toast mToast;
    private static Toast mShowToast;


    public static void showCustom(Context context, @StringRes int resId) {
        showCustom(context, context.getString(resId));
    }

    public static void showCustom(Context context, Object paramObject) {
        showCustom(context, paramObject, 0);
    }

    public static void showCustom(Context context, Object paramObject, int duration) {
        if (paramObject == null) {
            return;
        }
        toastContentView = LayoutInflater.from(context).inflate(R.layout.custom_toast_view, null);
        toastIconView = (ImageView) toastContentView.findViewById(R.id.toast_icon);
        toastMessageView = (TextView) toastContentView.findViewById(R.id.toast_message);
        toastMessageView.setText(paramObject.toString());
        if (mToastView==null) {
            mToastView = new Toast(context);
            if (duration == 0) {
                mToastView.setDuration(Toast.LENGTH_SHORT);
            } else {
                mToastView.setDuration(Toast.LENGTH_LONG);
            }
            mToastView.setGravity(Gravity.TOP, 0, DensityUtil.getScreenH(context) / 3);//获取屏幕高度
            mToastView.setView(toastContentView);
        }else {
            if (duration == 0) {
                mToastView.setDuration(Toast.LENGTH_SHORT);
            } else {
                mToastView.setDuration(Toast.LENGTH_LONG);
            }
            mToastView.setGravity(Gravity.TOP, 0, DensityUtil.getScreenH(context) / 3);//获取屏幕高度
            mToastView.setView(toastContentView);
        }
        mToastView.show();
    }

    public static void showCustomLong(Context context, Object paramObject) {
        showCustom(context, paramObject, 1);
    }

    public static void show(Context context, CharSequence text) {
        if (mToast == null){
            mToast =  Toast.makeText(context, text, Toast.LENGTH_SHORT);
        }else {
            mToast.setText(text);
        }
        mToast.show();
    }

    public static void show(Context context, @StringRes int resId) {
        Toast.makeText(context, context.getString(resId), Toast.LENGTH_SHORT).show();
    }

    /**
     * 事件类型 错误或者 正确 根据类型显示
     *
     * @param msg
     * @param type 0-代表图片不显示, 1-成功 ，2-失败
     */
    public static void showToast(Context context, String msg, boolean type) {
        // 获取布局相关
        View view = LayoutInflater.from(context).inflate(R.layout.custom_toast_layout, null);
        ImageView mImg = (ImageView) view.findViewById(R.id.flag_img);
        TextView mText = (TextView) view.findViewById(R.id.show_text);
        // 资源id
        int imgId = -1;
        // 根据type显示不同
        if (type) {
            imgId = R.drawable.succeeful;
        } else {
            imgId = R.drawable.so_bad;
        }
        mImg.setBackgroundResource(imgId);
        mImg.setVisibility(View.VISIBLE);
        mText.setText(msg);
        if (mShowToast==null){
            mShowToast = new Toast(context);
            mToast.setView(view);
            mToast.setGravity(Gravity.CENTER, 0, 0);
            mToast.setDuration(Toast.LENGTH_SHORT);
        }else {
            mToast.setView(view);
            mToast.setGravity(Gravity.CENTER, 0, 0);
            mToast.setDuration(Toast.LENGTH_SHORT);
        }
        mToast.show();
    }

    public static void showToast(Context context, @StringRes int resId, boolean type) {
        showToast(context,context.getString(resId),type);
    }


}
